﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Fa;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Fa
{
    public partial class frmMaintainSaleAsset : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        private FaAssetsEntity mSelectedAsset;
        private FaSaleAssetsEntity mAssetSalesInfo;
        #endregion

        #region Construction
        public frmMaintainSaleAsset(FaAssetsEntity asset)
        {
            InitializeComponent();
            mSelectedAsset = asset;
        }
        #endregion

        #region Function
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FAS", true);
        }
        private void SetAssetInfoFields()
        {
            if (mSelectedAsset == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedAsset.AssetTypeId) == false)
            {
                FaAssetsTypeEntity pt = FaAssetsTypeBs.GetFaAssetsTypeByID(mSelectedAsset.AssetTypeId);
                txtAssetType.Text = pt.AssetTypeName;
                txtAssetType.Tag = pt;
                if (string.IsNullOrEmpty(pt.ParentId) == false)
                {
                    FaAssetsTypeEntity pt2 = FaAssetsTypeBs.GetFaAssetsTypeByID(pt.ParentId);
                    txtAssetType02.Text = pt2.AssetTypeName;
                    txtAssetType02.Tag = pt2;
                }
            }
            if (string.IsNullOrEmpty(mSelectedAsset.ParentAsset) == false)
            {
                FaAssetsEntity fa = FaAssetsBs.GetByID(mSelectedAsset.AssetId);
                txtParentAsset.Text = fa.AssetName;
                txtParentAsset.Tag = fa;
            }
            txtAssetCode.Text = mSelectedAsset.AssetCode;
            txtAssetSpec.Text = mSelectedAsset.AssetSpec;
            txtAssetName.Text = mSelectedAsset.AssetName;
            txtManufacturer.Text = mSelectedAsset.Manufacturer;
            cmbStatus.SetSelectedValue(mSelectedAsset.Status);
            txtDescription.Text = mSelectedAsset.AssetDescription;

            txtPrice.Text = mSelectedAsset.Price.ToString("#0.00");
            txtProductiongCode.Text = mSelectedAsset.ProductionCode;
        }
        private void SetField()
        {
            SetAssetInfoFields();
            mAssetSalesInfo = FaSaleAssetsBs.GetById(mSelectedAsset.AssetId);
            if (mAssetSalesInfo == null)
            {
                txtSalesPerson.Text = "";
                txtSalesPerson.Tag = null;
                dtpSalesDate.EditValue = null;
                txtSalesPrice.Text = "";
                txtBuyer.Text = "";
                txtRemark.Text = "";
            }
            else
            {
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(mAssetSalesInfo.SalePerson);
                if (person != null)
                {
                    txtSalesPerson.Text = person.NameCn;
                    txtSalesPerson.Tag = person;
                }
                dtpSalesDate.EditValue = mAssetSalesInfo.SaleDate;
                txtSalesPrice.Text = mAssetSalesInfo.Price.ToString("#0.00");
                txtBuyer.Text = mAssetSalesInfo.Buyer;
                txtRemark.Text = mAssetSalesInfo.Remark;
            }
        }
        private bool SetEntity()
        {
            bool isNew = false;
            mAssetSalesInfo = FaSaleAssetsBs.GetById(mSelectedAsset.AssetId);
            if (mAssetSalesInfo == null)
            {
                mAssetSalesInfo = new FaSaleAssetsEntity();
                mAssetSalesInfo.AssetId = mSelectedAsset.AssetId;
                isNew = true;
            }
            if (dtpSalesDate.EditValue!=null)
            {
                mAssetSalesInfo.SaleDate =Convert.ToDateTime(dtpSalesDate.EditValue);
            }
            if (txtSalesPerson.Tag != null && txtSalesPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                mAssetSalesInfo.SalePerson = ((MstPersonEntity)txtSalesPerson.Tag).PersonId;
            }
            mAssetSalesInfo.Price = CommonFunctions.ToDecimal(txtSalesPrice.Text);
            mAssetSalesInfo.Buyer = txtBuyer.Text;
            mAssetSalesInfo.Remark = txtRemark.Text;
            return isNew;
        }
        private bool CheckData()
        {
            if (dtpSalesDate.EditValue == null)
            {
                MessageBox.Show("变卖日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtSalesPerson.Text))
            {
                MessageBox.Show("变卖人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtSalesPrice.Text))
            {
                MessageBox.Show("变卖金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                bool isNew = SetEntity();
                if (ThisActionType != ActionType.View)
                {
                    bool isSuccessful = FaAssetsBs.SaleAsset(mSelectedAsset, mAssetSalesInfo, isNew);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("变卖失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        private void frmMaintainSaleAsset_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetField();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtSalesPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtSalesPerson.Text = frm.SelectedPerson.NameCn + " (" + frm.SelectedPerson.PersonCode + ")";
                txtSalesPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtSalesPerson.Text = "";
                txtSalesPerson.Tag = null;
            }
        }




    }
}